package com.application.chiettinhdienluc.control;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import com.application.chiettinhdienluc.model.NhanCongChiTietModel;

public class NhanCongInfoListControl extends ArrayList<NhanCongChiTietModel> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2963755199959921000L;
	private volatile HashMap<String, NhanCongChiTietModel> hashNhanCongList = new HashMap<String, NhanCongChiTietModel>();

	@Override
	public void clear() {
		hashNhanCongList.clear();		
		super.clear();
	}
	
	@Override
	public boolean add(NhanCongChiTietModel object) {
		boolean value = false;
		if (hashNhanCongList.put(object.getMaCongViec(), object) == null) {
			value = super.add(object);
		}
		return value;
	}
	
	@Override
	public boolean addAll(Collection<? extends NhanCongChiTietModel> collection) {
		boolean value = false;
		for (NhanCongChiTietModel nhancongDetail : collection) {
			if(nhancongDetail != null)
			{
				if (hashNhanCongList.put(nhancongDetail.getMaCongViec(), nhancongDetail) == null) {
					value = true;
					value = super.add(nhancongDetail);
				}
			}
		}
		return value;
	}
	
	public int updateOrAdd(List<NhanCongChiTietModel> listFriend) {
		int resultValue = 0;
		for (NhanCongChiTietModel obj : listFriend) {
			
			if (contain(obj.getMaCongViec())) {
				NhanCongChiTietModel objOld = get(obj.getMaCongViec());
				resultValue++;
			} else {
				try {
					hashNhanCongList.put(obj.getMaCongViec(), obj);
					resultValue++;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return resultValue;
	}
	
	@Override
	public NhanCongChiTietModel remove(int index) {
		hashNhanCongList.remove(get(index).getMaCongViec());
		return super.remove(index);
	}
	
	@Override
	public boolean remove(Object object) {
		if (object instanceof NhanCongChiTietModel) {
			hashNhanCongList.remove(((NhanCongChiTietModel) object).getMaCongViec());
		}
		return super.remove(object);
	}
	
	public void clearFriendListCache() {

	}
	
	public boolean contain(String maCongViec) {
		return hashNhanCongList.containsKey(maCongViec);
	}
	
	public NhanCongChiTietModel get(String maCongViec) {
		return hashNhanCongList.get(maCongViec);
	}
}
